package ztc.servlet;

import ztc.common.Constant;
import ztc.common.SiginBean;
import ztc.common.StuBean;
import ztc.common.UserBean;
import ztc.service.DBTools;
import ztc.service.DBUsers;
import ztc.service.QRStatus;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ztc on 16-4-12.下午5:14
 */
public class SaveSiginStu extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action=request.getParameter("action");
        HttpSession session=request.getSession();
        ServletContext application=this.getServletContext();
        ConcurrentHashMap<String,ConcurrentHashMap<String,StuBean>> aMap=
                (ConcurrentHashMap<String, ConcurrentHashMap<String, StuBean>>) application.getAttribute("sessionID");
        ConcurrentHashMap<String,StuBean> sMap=
                aMap.get(session.getId());
        //先清除null
        QRStatus.rmNull(application,session);
        //再把sMap从aMap里删除
        aMap.remove(session.getId());
        SiginBean siginBean= (SiginBean) session.getAttribute("siginBean");
        session.removeAttribute("siginBean");
        if(action.equals("save")) {
            UserBean userBean = (UserBean) session.getAttribute("userBean");
            DBTools tools = new DBTools();
            tools.insertSigin(siginBean);
            if(tools.saveSiginStu(sMap, userBean.getId() + Constant.separator + siginBean.getSname()))
                response.sendRedirect("/qr/showinf.jsp?msg="+ URLEncoder.encode("保存成功！","utf-8"));
            else
                response.sendRedirect("/qr/showinf.jsp?msg="+ URLEncoder.encode("保存失败！","utf-8"));
        }else if(action.equals("delete")){
            response.sendRedirect("/qr/showinf.jsp?msg="+ URLEncoder.encode("删除成功！","utf-8"));
        }
    }
}
